package com.ustc.emailcheck;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

import com.ustc.emailUI.ConfigureInfoUI;
import com.ustc.emailUI.NewEmailNotifyUI;
import com.ustc.emailtray.Tray;

/* Monitors given mailbox for new mail */

public class Monitor{

	private static final int INTERVAL=1*10*1000;
	private static final int WAIT=3000;
	private String emailclient;
	private Tray tray;
	private Account[] accounts;
	
	public Monitor(){
		MouseListener mouseListener_on=new MouseListener(){

			@Override
			public void mouseClicked(MouseEvent e) {
				newMailHandler();				
			}
			

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
				
			}
 
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		};
		MouseListener mouseListener_off=new MouseListener(){

			@Override
			public void mouseClicked(MouseEvent e) {
				
				if(e.isMetaDown()){
					 new ConfigureInfoUI().showMainPageUI();
				}
				
			}
			

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
				
			}
 
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		};
		tray=new Tray(mouseListener_on, mouseListener_off);
		tray.showTray(false);
		this.check(INTERVAL);
	}
	
	public static void main(String[] args) {
		File accountfile=new File(Account.ACCOUNT_FILE);
		if(!accountfile.exists()){
			new ConfigureInfoUI().showConfigureUI();
		}
		new Monitor();
	}
	
	public void check(long interval){
		File accountfile=new File(Account.ACCOUNT_FILE);
		Properties props = System.getProperties();
		int count,current;
		while(true){
			try {				
				if(!accountfile.exists()){
					System.out.println("account not created!");
					Thread.sleep(interval);
					continue;
				}
				accounts=Account.loadAccount();
				for(Account account:accounts){						
					Session session = Session.getInstance(props, null);
					Store store = session.getStore(account.getServerType());
					store.connect(account.getServer(),account.getUsername(),account.getPassword());
					Folder folder = store.getFolder("INBOX");
					count=account.getCount();
					
			    	folder.open(Folder.READ_ONLY);
			    	current=folder.getMessageCount();
			    	folder.close(true);
			    	if(count<current){
			    		emailclient=account.getEmailclient();
			    		account.setCount(current);
			    		tray.showTray(true);
			    		NewEmailNotifyUI ui=new NewEmailNotifyUI();
			    		ui.showNewEmailNotifyUI(account.getUsername());
			    		Thread.sleep(WAIT);
			    		ui.getNewEmailNotifyJframe().setVisible(false);
			    	}
				}
				Thread.sleep(interval);
				
				
			} catch (NoSuchProviderException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
	}
	
	public void newMailHandler(){
		try {
			Runtime.getRuntime().exec(emailclient);
			tray.showTray(false);
			Account.saveAccount(accounts);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
